<%--
  Created by IntelliJ IDEA.
  User: xie392
  Date: 2023/6/7
  Time: 21:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.jwxt.DatabaseQuery" %>
<%@ page import="com.jwxt.Crypto" %>
<%@ page import="java.util.List" %>
<%@ page import="com.jwxt.Use" %>

<%
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String password1 = request.getParameter("password1");
    String password2 = request.getParameter("password2");

    // 判断两次密码是否一致
    if (!password1.equals(password2)) {
        session.setAttribute("message", "两次密码不一致");
        response.sendRedirect("../pages/register.jsp");
        return;
    }

    // 判断用户名是否已经存在
    List<List<String>> resultUserName = DatabaseQuery.query("select * from webuser where userName = '" + name + "'");
    if (resultUserName != null && resultUserName.size() != 0) {
        session.setAttribute("message", "用户名已经存在");
        response.sendRedirect("../pages/register.jsp");
        return;
    }

    // 判断邮箱是否已经存在
    List<List<String>> resultEmail = DatabaseQuery.query("select * from webuser where email = '" + email + "'");
    if (resultEmail != null && resultEmail.size() != 0) {
        session.setAttribute("message", "邮箱已经存在");
        response.sendRedirect("../pages/register.jsp");
        return;
    }

    // 插入数据
    String sql = "insert into webuser(uid, userName, email, password, registDate) values('" + Use.generateId() + "','" + name + "', '" + email + "', '" + Crypto.encrypt(password1) + "','" + Use.getCurrentTimeString() + "');";
    boolean result = DatabaseQuery.execute(sql);

    // 判断是否插入成功
    if (!result) {
        session.setAttribute("message", "注册失败");
        response.sendRedirect("../pages/register.jsp");
    } else {
        session.setAttribute("message", "注册成功");
        response.sendRedirect("../pages/login.jsp");
    }

%>
